package com.orderMgr.biz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.orderMgr.bean.OfFood;
import com.orderMgr.bean.OfFoodCriteria;
import com.orderMgr.bean.OfOrderDetail;
import com.orderMgr.bean.OfOrderDetailCriteria;
import com.orderMgr.bean.OfRestaurant;
import com.orderMgr.bean.OfRestaurantCriteria;
import com.orderMgr.bean.UserOrderFood;
import com.orderMgr.dao.OfFoodDAO;
import com.orderMgr.dao.OfOrderDetailDAO;
import com.orderMgr.dao.OfRestaurantDAO;
import com.usermgr.bean.OfUser;
import com.usermgr.bean.OfUserCriteria;
import com.usermgr.dao.OfUserDAO;

/**
 *@title      	标题：
 *@description 	描述：
 *@author 	  	作者 ：wuxionghao
 *@createTime 	创建时间：2012-1-4上午10:59:16
 *@version    	版本：v1.0
 */
public class OrderMgrImpl implements IOrderMgr {

	private OfRestaurantDAO ofRestaurantDAO;
	private OfUserDAO ofUserDAO;
	private OfOrderDetailDAO ofOrderDetailDAO;
	private OfFoodDAO ofFoodDAO;
	
	/**
	 * 用户订餐
	 */
	@SuppressWarnings("unchecked")
	public boolean userOrderFood(String userName, Long foodId) {
		OfUserCriteria ouc = new OfUserCriteria();
		if(userName == null){
			ServletActionContext.getRequest().setAttribute("msg","您没有登录 ！请先登录...");
			return false;
		}else{
		ouc.createCriteria().andOfNameEqualTo(userName);
		List<OfUser> users = ofUserDAO.selectByExample(ouc);
		Long userId = users.get(0).getOfId();
		OfOrderDetail orderDetail = new OfOrderDetail();
		orderDetail.setOfUserId(userId);
		orderDetail.setOfFoodId(foodId);
		orderDetail.setOfTime(new Date());
		ofOrderDetailDAO.insert(orderDetail);
		return true;
		}
	}
	/**
	 * 显示今日订餐
	 */
	@SuppressWarnings("unchecked")
	public List<List> showToDayOrdered() {
		List<List> list = new ArrayList<List>();
		OfOrderDetailCriteria example = new OfOrderDetailCriteria();
		example.setOrderByClause("of_time desc");
		example.createCriteria().andOfUserIdIsNotNull()
								.andOfTimeBetween(getNowTime(new Date()), new Date());
		List<OfOrderDetail> detailList = ofOrderDetailDAO.selectByExample(example);
		for(int i = 0 ; i < detailList.size(); i++){
			OfOrderDetail  detail = detailList.get(i);
			OfFood ofFood = ofFoodDAO.selectByPrimaryKey(detail.getOfFoodId());
			OfUser user = ofUserDAO.selectByPrimaryKey(detail.getOfUserId());
			OfRestaurant ofRestaurtant=ofRestaurantDAO.selectByPrimaryKey(ofFoodDAO.selectByPrimaryKey(detail.getOfFoodId()).getOfRestaurantId());
			SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List detai = new ArrayList();
			
			detai.add(ofFood.getOfFoodName());
			detai.add(ofFood.getOfFoodPrice());
			
			detai.add(user.getOfName());
			detai.add(simp.format(detail.getOfTime()));
			
			detai.add(detail.getOfId());
			detai.add(detail.getOfUserId());
			
			detai.add(ofRestaurtant.getOfName());
			
			detai.add(ofRestaurtant.getOfTel());
			list.add(detai);
		}
		return list;
	}
	//显示所有的订餐
	@SuppressWarnings("unchecked")
	public List<List> showAllOrdered() {
		List<List> list = new ArrayList<List>();
		OfOrderDetailCriteria example = new OfOrderDetailCriteria();
		example.createCriteria().andOfFoodIdIsNotNull();
		List<OfOrderDetail> detailList = ofOrderDetailDAO.selectByExample(example);
		for(int i=0;i<detailList.size();i++){
			OfOrderDetail  detail = detailList.get(i);
			OfFood ofFood = ofFoodDAO.selectByPrimaryKey(detail.getOfFoodId());
			
			OfUser user = ofUserDAO.selectByPrimaryKey(detail.getOfUserId());
			OfRestaurant ofRestaurtant=ofRestaurantDAO.selectByPrimaryKey(ofFoodDAO.selectByPrimaryKey(detail.getOfFoodId()).getOfRestaurantId());
			SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List detai = new ArrayList();
			detai.add(ofFood.getOfFoodName());
			detai.add(ofFood.getOfFoodPrice());
			
			detai.add(user.getOfName());
			detai.add(simp.format(detail.getOfTime()));
			
			detai.add(detail.getOfId());
			detai.add(detail.getOfUserId());
			
			detai.add(ofRestaurtant.getOfName());
			
			detai.add(ofRestaurtant.getOfTel());
			list.add(detai);
		}
		return list;
	}
	
	public List<OfFood> showAllFood() {
		return ofFoodDAO.showAllFood();
	}
	/**
	 * 显示所有的菜品信息
	 */
	@SuppressWarnings("unchecked")
	public List<List> getFoodInfo() {
		List<List> list=new ArrayList<List>();
		OfFoodCriteria example =new OfFoodCriteria ();
		example.createCriteria().andOfIdIsNotNull();
		List<OfFood> foodList=ofFoodDAO.selectByExample(example);
		for(int i=0;i<foodList.size();i++){
			OfFood food=foodList.get(i);
			OfRestaurant ofRestaurtant=ofRestaurantDAO.selectByPrimaryKey(food.getOfRestaurantId());
			List info=new ArrayList();
			//菜品id
			info.add(food.getOfId());
			//菜品名称
			info.add(food.getOfFoodName());
			//菜品价格
			info.add(food.getOfFoodPrice());
			//菜品图片
			info.add(food.getOfFoodPic());
			//餐馆id
			info.add(food.getOfRestaurantId());
			//餐馆名称
			info.add(ofRestaurtant.getOfName());
			//餐馆电话
			info.add(ofRestaurtant.getOfTel());
			list.add(info);
		
		}
		return list;
	}
	
	public boolean AddFood(OfFood ofFood) {
		return ofFoodDAO.AddFood(ofFood);
	}
	public int deleteFood(long ofId) {
		
		return ofFoodDAO.deleteByPrimaryKey(ofId);
	}
	
	
	
	/**
	 * 删除用户订餐
	 */
	public boolean userDelFood(long detailId,Long userId) {
		boolean result = false;
		OfOrderDetail detail = ofOrderDetailDAO.selectByPrimaryKey(detailId);
		OfUser user = ofUserDAO.selectByPrimaryKey(userId);
		String permission = user.getOfPermission();
		if(detail.getOfUserId().equals(userId) || permission.equals("1")){
			if(ofOrderDetailDAO.deleteByPrimaryKey(detailId) == 1){
				result = true; 
			}
		}
		return result;
	}
	/**
	 * 显示用户点餐
	 * @author meishuangan
	 */
	public List<UserOrderFood> getUserOrderFood() {
		return ofOrderDetailDAO.getUserOrderFood();
	}
	/**
	 * 显示当天用户点餐
	 */
	public List<UserOrderFood> getTodayUserOrderFood() {
		return ofOrderDetailDAO.getTodayUserOrderFood();
	}
	/**
	 * 通过用户名称搜索点餐
	 */
	public List<UserOrderFood> getOrderByFoodName(String foodName) {
		return ofOrderDetailDAO.getOrderByFoodName(foodName);
	}
	/**
	 * 通过菜单名字搜索点餐
	 */
	public List<UserOrderFood> getOrderByUserName(String userName) {
		return ofOrderDetailDAO.getOrderByUserName(userName);
	}
	
	/**
	 * 取得当天00：00：00秒这个时间
	 * @param date
	 * @return
	 */
	public Date getNowTime(Date date) {
		SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat simp2 = new SimpleDateFormat("yyyy-MM-dd");
		
		String time = simp2.format(date);
		time = time+" 00:00:00";
		Date todayTime = new Date();
		try {
			todayTime = simp.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return todayTime;
	}
	public OfRestaurantDAO getOfRestaurantDAO() {
		return ofRestaurantDAO;
	}

	public void setOfRestaurantDAO(OfRestaurantDAO ofRestaurantDAO) {
		this.ofRestaurantDAO = ofRestaurantDAO;
	}

	
	public OfUserDAO getOfUserDAO() {
		return ofUserDAO;
	}

	public void setOfUserDAO(OfUserDAO ofUserDAO) {
		this.ofUserDAO = ofUserDAO;
	}

	public OfOrderDetailDAO getOfOrderDetailDAO() {
		return ofOrderDetailDAO;
	}

	public void setOfOrderDetailDAO(OfOrderDetailDAO ofOrderDetailDAO) {
		this.ofOrderDetailDAO = ofOrderDetailDAO;
	}

	public OfFoodDAO getOfFoodDAO() {
		return ofFoodDAO;
	}

	public void setOfFoodDAO(OfFoodDAO ofFoodDAO) {
		this.ofFoodDAO = ofFoodDAO;
	}

	
	
}
